#include "Restaurant.h"
#include <stdio.h>
#include <assert.h>
#include <iostream>
#include <conio.h>
using namespace std;
#define NAME_LENGTH 50

ErrorType showStartup(int* select){
	cout<<"\nPlease select an option: \n";
	cout<<"1 - Create a new customer and order\n";
	cout<<"2 - Update an order of an existing customer \n";
	cout<<"3 - See the order of an existing customer \n";
	cout<<"4 - Delete the order of an existing customer \n";
	cout<<"5 - Delete a customer \n";
	cout<<"6 - Exit \n";
	int subSelect;
	cin>>subSelect;
	while (subSelect<1 || subSelect>6){
		cout<<"\nPlease select a VALID option: \n";
		cout<<"1 - Create a new customer \n";
		cout<<"2 - Update an order of an existing customer \n";
		cout<<"3 - See the order of an existing customer \n";
		cout<<"4 - Delete the order of an existing customer \n";
		cout<<"5 - Delete a customer \n";
		cout<<"6 - Exit \n";
		cin>>subSelect;
	}
	*select = subSelect;
	return OK;
}

ErrorType createOrder(char* name, DrinkType *drink,FirstType *firstCourse,MainType *mainCourse,DesertType *desert){
	int subSelect;
	// DRINK
	cout<<"\nWhat would you like to drink? \n";
	cout<<"0 - Nothing \n";
	cout<<"1 - Coca Cola \n";
	cout<<"2 - Sprite \n";
	cin>>subSelect;
	while (subSelect<0 || subSelect>2){
		cout<<"\nPlease select a VALID option: \n";
		cout<<"0 - Nothing \n";
		cout<<"1 - Coca Cola \n";
		cout<<"2 - Sprite \n";
		cin>>subSelect;
	}

	switch(subSelect)
	{
	case 0: {
		*drink = NODRINK;
			break;
			}
	case 1:{
		*drink = COLA;
		break;
			}
	case 2:{
		*drink = SPRITE;
		break;
			}
	}
	// FIRST
	cout<<"\nWhat would you like for your first course? \n";
	cout<<"0 - Nothing \n";
	cout<<"1 - Soup \n";
	cout<<"2 - Hummus \n";
	cin>>subSelect;
	while (subSelect<0 || subSelect>2){
		cout<<"\nPlease select a VALID option: \n";
		cout<<"0 - Nothing \n";
		cout<<"1 - Soup \n";
		cout<<"2 - Hummus \n";
		cin>>subSelect;
	}

	switch(subSelect)
	{
	case 0: {
		*firstCourse = NOFIRST;
		break;
			}
	case 1:{
		*firstCourse = SOUP;
		break;
			}
	case 2:{
		*firstCourse = HUMMUS;
		break;
			}
	}

	// MAIN
	cout<<"\nWhat would you like for your main course? \n";
	cout<<"0 - Nothing \n";
	cout<<"1 - Steak \n";
	cout<<"2 - Hamburger \n";
	cin>>subSelect;
	while (subSelect<0 || subSelect>2){
		cout<<"\nPlease select a VALID option: \n";
		cout<<"0 - Nothing \n";
		cout<<"1 - Steak \n";
		cout<<"2 - Hamburger \n";
		cin>>subSelect;
	}

	switch(subSelect)
	{
	case 0: {
		*mainCourse = NOMAIN;
		break;
			}
	case 1:{
		*mainCourse = STEAK;
		break;
			}
	case 2:{
		*mainCourse = HAMBURGER;
		break;
			}
	}

	// DESERT
	cout<<"\nWhat would you like for your desert? \n";
	cout<<"0 - Nothing \n";
	cout<<"1 - Cake \n";
	cout<<"2 - Bamba \n";
	cin>>subSelect;
	while (subSelect<0 || subSelect>2){
		cout<<"\nPlease select a VALID option: \n";
		cout<<"0 - Nothing \n";
		cout<<"1 - Cake \n";
		cout<<"2 - Bamba \n";
		cin>>subSelect;
	}

	switch(subSelect)
	{
	case 0: {
		*desert = NODESERT;
		break;
			}
	case 1:{
		*desert = CAKE;
		break;
			}
	case 2:{
		*desert = BAMBA;
		break;
			}
	}

	return OK;
}

ErrorType printOrder(char* name, DrinkType drink,FirstType firstCourse,MainType mainCourse,DesertType desert){
	char* drinkSTR = new char[NAME_LENGTH];
	char* firstSTR = new char[NAME_LENGTH];
	char* mainSTR = new char[NAME_LENGTH];
	char* desertSTR = new char[NAME_LENGTH];
	switch(drink){
		case NODRINK:{drinkSTR = "Nothing";	break;}
		case COLA:{drinkSTR = "Coca Cola";	break;}
		case SPRITE:{drinkSTR = "Sprite";	break;}
	}
	switch(firstCourse){
		case NOFIRST:{firstSTR = "Nothing";	break;}
		case SOUP:{firstSTR = "Soup";	break;}
		case HUMMUS:{firstSTR = "Hummus";	break;}
	}
	switch(mainCourse){
		case NOMAIN:{mainSTR = "Nothing";	break;}
		case STEAK:{mainSTR = "Steak";	break;}
		case HAMBURGER:{mainSTR = "Hamburger";	break;}
	}
	switch(desert){
		case NODESERT:{desertSTR = "Nothing";	break;}
		case CAKE:{desertSTR = "Cake";	break;}
		case BAMBA:{desertSTR = "Bamba";	break;}
	}
	cout<<name;
	cout<<" ordered: ";
	cout<<drinkSTR;
	cout<<", ";
	cout<<firstSTR;
	cout<<", ";
	cout<<mainSTR;
	cout<<" and ";
	cout<<desertSTR;
	cout<<".";
	return OK;
}


int main(int argc, const char* argv[]){
	bool exit = false;
	int select;
	ErrorType res;
	
	RestaurantSystem *restaurant = new RestaurantSystem();
	cout<<"Welcome dear diner!\nEnjoy our disgausting food\n";
	
	while(!exit){
		res = showStartup(&select);
		assert(res==OK);
		char* name = new char[NAME_LENGTH];
		DrinkType drink;
		FirstType firstCourse;
		MainType mainCourse;
		DesertType desert;

		switch(select)
		{
			case 1: {
				// Create customer by name
				cout<<"Please insert the customer's name: ";
				cin>>name;
				res= restaurant->addCustomer(name);
				assert(res==OK);
				res = createOrder(name, &drink, &firstCourse, &mainCourse, &desert);
				res = restaurant->order(name, drink, firstCourse, mainCourse, desert);
				assert(res==OK);
				cout<<"Order received successfully.\nThe system will now return to the main menu.\n\n";
				break;
			}
			case 2: {
				cout<<"Please insert the customer's name: ";
				cin>>name;
				res = restaurant->order(name, NODRINK, NOFIRST, NOMAIN, NODESERT);
				if (res==NOT_FOUND_ERROR){
					cout<<"Customer not found.";
					break;
				}
				res = createOrder(name, &drink, &firstCourse, &mainCourse, &desert);
				res = restaurant->order(name, drink, firstCourse, mainCourse, desert);
				assert(res==OK);
				cout<<"Order received successfully.\nThe system will now return to the main menu.\n\n";
				break;
				
			}
			case 3: {
				cout<<"Please insert the customer's name: ";
				cin>>name;
				DrinkType& drinkREF = drink;
				FirstType& firstCourseREF = firstCourse;
				MainType& mainCourseREF = mainCourse;
				DesertType& desertREF = desert;
				res = restaurant->getOrder(name, drinkREF, firstCourseREF, mainCourseREF, desertREF);
				if (res==NOT_FOUND_ERROR){
					cout<<"Customer not found.";
					break;
				}
				res = printOrder(name, drink, firstCourse, mainCourse, desert);
				assert(res==OK);
				break;
				
			}
			case 4: {
				cout<<"Please insert the customer's name: ";
				cin>>name;
				res = restaurant->order(name, NODRINK, NOFIRST, NOMAIN, NODESERT);
				if (res==NOT_FOUND_ERROR){
					cout<<"Customer not found.";
					break;
				}
				cout<<"Order Deleted successfully.\nThe system will now return to the main menu.\n\n";
				break;
			}
			case 5: {
				cout<<"Please insert the name of the customer you want to delete: ";
				cin>>name;
				res = restaurant->deleteCustomer(name);
				if (res==NOT_FOUND_ERROR){
					cout<<"Customer not found.";
					break;
				}
				cout<<"Customer deleted.\nThe system will now return to the main menu.\n\n";
				
				break;
			}
			case 6: {
				cout<<"\nThank you. Bye!\n ";
				exit = true;
				break;
			}
		}
		cout<<"\nPress any key to continue\n";
		_getch();
	}

	delete restaurant;
	return 0;
};

